package com.wngbms.guava;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest {


    public static void main(String[] args) throws InterruptedException {
//        m1();
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        lock.lock();
        try {
            new Thread(() -> {
                lock.lock();
                System.out.println("================");
                try {
//                    Thread.sleep(Integer.MAX_VALUE);
                    Thread.sleep(15 * 1000);
//                    Thread.sleep(1 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                condition.signal();
                System.out.println("after signal");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("before unlock");
                lock.unlock();
            }).start();



            Thread.sleep(1000);

//            condition.await();
            condition.await(5, TimeUnit.SECONDS);
            System.out.println(">>>>>>>>>>>>>>>>>");
        } finally {
            lock.unlock();
        }

    }

    private static void m1() throws InterruptedException {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        lock.lock();
        try {
            Thread.sleep(1000);

            new Thread(() -> {
                lock.lock();
                System.out.println("================");
                lock.unlock();
            }).start();

            Thread.sleep(1000);

            new Thread(() -> {
                lock.lock();
                System.out.println("================");
                lock.unlock();
            }).start();
        } finally {
            lock.unlock();
        }
    }
}
